<template>
	<view class="page quenaire">
		<view class="question-box">
			<view class="videoShow" v-if="question.type == 'video'">
				<view class="videoBtn" v-if="videoSrc != ''">
					<video style="width:560rpx;" :src="videoSrc"></video>
					<image src="https://file.casugn.com/storecompcard/temp/cardholderIcon_1654821847251.png"
						mode="widthFix" @click="handleVideoDel"></image>
				</view>
				<view class="videoUpShow showpic" v-if="videoSrc == ''">
					<image
						src="https://file.casugn.com/storecompcard//sbr11r9ji8lmd1bbba35f5324c1da5b910e53f3d8ac5_1681285322874.png"
						@click="videoAdd"></image><br />
					<text>点击上传视频</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				content: '',
				videoContent: '',
				compress: true,
				videoSrc: '',
				cameraList: [{
						value: 'back',
						name: '后置摄像头',
						checked: 'true'
					},
					{
						value: 'front',
						name: '前置摄像头'
					},
				],
				cameraIndex: 0,
			}
		},
		methods: {
			handleVideoDel() {
				this.videoSrc = '';
				this.videoContent = '';
			},
			videoAdd() {
				uni.chooseVideo({
					maxDuration: 60, // 最大为60秒
					count: 1,
					compressed: true,
					camera: this.cameraList[this.cameraIndex].value,
					sourceType: ['album', 'camera'],
					success: (res) => {
						let videoFile = res.tempFilePath;
						this.videoSrc = res.tempFilePath;
						console.log("res", res);
						if (this.compress && (res.size / 10240 > 1025)) {
							console.log("压缩")
							this.videoCompress(res.tempFilePath);
							console.log("压缩后")
						} else {
							this.videoUpload(res.tempFilePath);
						}
					}
				})
			},
			videoCompress(tempFilePath) {
				uni.showLoading({
					title: '压缩中...'
				});
				var that = this;
				uni.compressVideo({
					src: tempFilePath,
					quality: 'low', //'low':低，'medium':中，'high':高  
					success: function(res) {
						console.log('压缩后', res)
						that.videoUpload(res.tempFilePath);
					},
					fail: function(err) {
						uni.showToast({
							title: '视频压缩失败',
							icon: 'none'
						}, 2000)
					}
				})
			},
			videoUpload(tempFilePath) {
				uni.showLoading({
					title: '上传中...'
				});
				const uploadTask = uni.uploadFile({
					url: this.baseUrl, // 自己文件上传的路径
					method: "POST",
					filePath: tempFilePath,
					name: 'file',
					success: (res) => {
						this.videoContent = JSON.parse(res.data).result.visiturl
						let resMessage = JSON.parse(res.data).message;
						if (JSON.parse(res.data).result.visiturl) {
							// uni.hideLoading();
							uni.showToast({
								title: resMessage,
								icon: 'none'
							})
						}
					},
					fail: (err) => {
						console.log(err);
						uni.hideLoading();
						uni.showToast({
							title: err,
							icon: 'none'
						})
						reject(err);
					},
					complete: () => {}
				})
				uploadTask.onProgressUpdate((res) => {
					uni.showLoading({
						title: '上传中' + res.progress + '%'
					});
					if (res.progress == 100) {
						uni.hideLoading();
					}
				});
			},
		},
	}
</script>